package com.unhz.taigu.controller;


import java.io.IOException;
import java.util.*;

import com.unhz.generator.enums.EnumStatus;
import com.unhz.generator.enums.EnumValid;
import com.unhz.generator.pojo.Insole;
import com.unhz.generator.pojo.InsoleDict;
import com.unhz.generator.pojo.InsoleImage;
import com.unhz.generator.pojo.*;
import com.unhz.generator.utils.*;
import com.unhz.taigu.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;



/**
 * @Author: 魏运奇
 * @Description: 中底样品
 * @Date: Created in 上午 9:37 2018/1/29 0029
 * @Modified By: V1.0.0
 */
@Controller
@RequestMapping("/insole")
public class InsoleController {

        @Autowired
        private InsoleService insoleService;

        @Autowired
        private InsoleDictService insoleDictService;

        @Autowired
        private InsoleImageService insoleImageService;

        @Autowired
        private ExpandImageService expandImageService;

        @Autowired
        private ReportImageService reportImageService;

        @Autowired
        private KeyService keyService;

        @Autowired
        private SysRolePermissionService sysRolePermissionService;

          /**
           * @Param pageNo 页数
           * @Param keywords  关键词
           * */
          @RequestMapping("/selectList")
          public ModelAndView selectInsole(Model model,@RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                            @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords,
                                            @RequestParam(value = "roleId",defaultValue = "",required = false)Integer roleId){
              System.out.print("成功进入控制器");
              ModelAndView mav=new ModelAndView("insole/index.jsp");
              Page<Insole> page =insoleService.selectInsoleList(pageNo, PageSize.pageSize,keywords,roleId);
              mav.addObject("page",page);
              int count = sysRolePermissionService.selectInsoleByroleId(roleId);
              mav.addObject("count",count);
              List<Insole> list=page.getRecords();
              return mav;
          }

        /**
         * 进入中底(传统、注塑、PU一体)详情页
         * @param mav
         * @param id
         * @return
         */
            @RequestMapping("/detail")
//            @RequiresPermissions("insole:find")
            public ModelAndView detail(ModelAndView mav,@RequestParam(value = "id")Integer id){
                 Insole insole = insoleService.selectByPrimaryKey(id);
                 String midsole = insole.getMidsole();
                 //查看传统中底详情
                 if("T".equals(midsole)) {
                      mav = new ModelAndView("insole/tradition/details.jsp");
                 }
                 //查看注塑中底详情
                if("P".equals(midsole)) {
                     mav = new ModelAndView("insole/injection/details.jsp");
                //查看PU中底详情
                 }if("R".equals(midsole)){
                     mav = new ModelAndView("insole/resin/details.jsp");
                }

                 mav.addObject("insole",insole);
                 //通过中底id查询中底图片信息
                 String[] insoleImages=insoleService.selectImageById(id);
                //通过中底id查询中底展开图片信息
                 String[] unfoldImgs=expandImageService.selectImageById(id);
                //通过中底id查询质检图片信息
                String[] reports=reportImageService.selectImageById(id);

                mav.addObject("insoleImage",insoleImages);
                mav.addObject("unfoldImgs",unfoldImgs);
                mav.addObject("reports",reports);

                 for(String str:insoleImages){
                     System.out.print("图片路径为："+str);
                 }
                 return  mav;
            }

        /**
         * 删除
         * @param id
         * @return
         */
        @RequestMapping("/delete")
             @ResponseBody
             public JsonResult delete(@RequestParam(value = "id",required = false)String id){
                boolean flag=insoleService.deleteById(Integer.parseInt(id));
              if(flag){
                  return  JsonResult.success("删除成功！");
              }
                return JsonResult.failure("删除失败！");
             }

        /**
         * 进入中底修改页(传统、注塑、PU一体)
         * @param model
         * @param insoleId
         * @return
         */
             @RequestMapping("/updateDetail")
//             @RequiresPermissions("insole:update")
             public String updateDetail(Model model,@RequestParam(value = "insoleId")Integer insoleId){
                 List<InsoleDict> traditionList = insoleDictService.selectInsoleByTradition();
                 List<InsoleDict> injectionList = insoleDictService.selectInsoleByInjection();
                 List<InsoleDict> resinList = insoleDictService.selectInsoleByResin();
                 Insole insole = insoleService.selectByPrimaryKey(insoleId);
                 List<InsoleImage> insoleImages = insoleImageService.selectInsoleImageList(insoleId);
                 List<ExpandImage> expandImages = expandImageService.selectExpandImageList(insoleId);
                 List<Report> reportImages = reportImageService.selectReportImageList(insoleId);
                 model.addAttribute("insoleImages",insoleImages);
                 model.addAttribute("expandImages",expandImages);
                 model.addAttribute("reportImages",reportImages);
                 model.addAttribute("traditionList",traditionList);
                 model.addAttribute("injectionList",injectionList);
                 model.addAttribute("resinList",resinList);
                 model.addAttribute("insole",insole);
                 return "insole/update.jsp";
             }

        /**
         * 执行禁用或者启用
         * @param insoleId
         * @param isenable
         * @param adminName
         * @return
         */
             @RequestMapping("/changeStatus")
             @ResponseBody
//             @RequiresPermissions("insole:isenable")
             public JsonResult changeStatus(@RequestParam("insoleId")Integer insoleId,
                                            @RequestParam("isenable")Integer isenable,
                                            @RequestParam("adminName")String adminName){
                 String index = insoleService.changsStatus(insoleId, isenable,adminName);
                 if("success".equals(index)){
                     return JsonResult.success("操作成功");
                 }
                 return JsonResult.failure("操作失败");
             }


        /**
         * 改变中底审核状态
         * @param insoleId
         * @param examine
         * @param adminName
         * @return
         */
        @RequestMapping(value = "/changeCheckStatus",method = RequestMethod.POST)
        @ResponseBody
//        @RequiresPermissions("insole:examine")
        public JsonResult changeCheckStatus(@RequestParam("insoleId")Integer insoleId,
                                            @RequestParam("examine")Integer examine,
                                            @RequestParam("adminName")String adminName){
            String index = insoleService.changeCheckStatus(insoleId, examine,adminName);
            if("success".equals(index)){
                return JsonResult.success("操作成功");
            }
            return JsonResult.failure("操作失败");
        }


        /**
         * 查询鞋子类型(男鞋、女鞋、童鞋)
         * @param insoleId
         * @return
         */
        @RequestMapping("/selectShoes")
        @ResponseBody
        public Insole selectShoes(@RequestParam(value = "insoleId",defaultValue = "0",required = true) Integer insoleId){
            Insole insole = insoleService.selectShoes(insoleId);
            return insole;
        }


        /**
         * 进入中底添加页面
         * @param model
         * @return
         */
        @RequestMapping("/addInsole")
//        @RequiresPermissions("insole:add")
        public String addInsole(Model model){
            List<InsoleDict> traditionList = insoleDictService.selectInsoleByTradition();
            List<InsoleDict> injectionList = insoleDictService.selectInsoleByInjection();
            List<InsoleDict> resinList = insoleDictService.selectInsoleByResin();
            model.addAttribute("traditionList",traditionList);
            model.addAttribute("injectionList",injectionList);
            model.addAttribute("resinList",resinList);
            return "insole/add.jsp";
        }

        /**
         * 新增保存中底样品信息
         * @param insole
         * @param insoleImgs
         * @param unfoldImgs
         * @param reports
         * @return
         */
        @RequestMapping("/uploadInsole")
        public String uploadInsole(Insole insole,
                                   @RequestParam(value="insoleImg",required = false)MultipartFile[] insoleImgs,
                                   @RequestParam(value="unfoldImg",required = false)MultipartFile[] unfoldImgs,
                                   @RequestParam(value="report",required = false)MultipartFile[] reports,
                                   @RequestParam(value = "adminName",required = false)String adminName) {
            Key key = keyService.selectKey();
            int insoleKey = key.getInsoleKey();
            if(insoleKey==1){
                insole.setValid(EnumValid.ENABLED.getIndex());
                insole.setStatus(EnumStatus.PASS.getIndex());
            }else{
                insole.setValid(EnumValid.FIRST_ENABLED.getIndex());
                insole.setStatus(EnumStatus.CHECKING.getIndex());
            }
            insole.setCreateBy(adminName);
            String flag=insoleService.addInsole(insole,insoleImgs,unfoldImgs,reports);
               if(flag.equals("success")){
                   return "redirect:selectList";
               }else{
                   return "添加中底失败";
               }
        }

        /**
         * 一键审核
         * @return
         */
        @RequestMapping("/keyPass")
        @ResponseBody
        public JsonResult keyPass(){
            Insole insole = new Insole();
            insole.setValid(1);
            insole.setStatus(2);
            insoleService.keyPass(insole);
            Key key = new Key();
            key.setId(1);
            key.setInsoleKey(1);
            keyService.updateKey(key);
            return JsonResult.success("操作成功");
        }

        /**
         * 取消一键审核
         * @return
         */
        @RequestMapping("/noKeyPass")
        @ResponseBody
        public JsonResult noKeyPass(){
            Key key = new Key();
            key.setId(1);
            key.setInsoleKey(0);
            keyService.updateKey(key);
            return JsonResult.success("操作成功");
        }

        /**
         * 查询一键审核标识符
         * @return
         */
        @RequestMapping("/selectInsoleKey")
        @ResponseBody
        public Key selectInsoleKey(){
            Key key = keyService.selectKey();
            return key;
        }

    /**
     * 修改中底信息
     * @param insole
     * @param adminName
     * @param insoleImgs
     * @param unfoldImgs
     * @param reports
     * @return
     */
        @RequestMapping("/updateInsole")
        public String updateInsole(Insole insole,
                                   @RequestParam(value="adminName",required = false)String adminName,
                                   @RequestParam(value="insoleImg",required = false)MultipartFile[] insoleImgs,
                                   @RequestParam(value="unfoldImg",required = false)MultipartFile[] unfoldImgs,
                                   @RequestParam(value="report",required = false)MultipartFile[] reports) {
            String flag = insoleService.updateInsole(insole,adminName,insoleImgs,unfoldImgs,reports);
            if(flag.equals("success")){
                return "redirect:selectList";
            }else{
                return "修改中底失败";
            }
        }

}
